<?php
namespace app\common\model;

use Exception;
use think\Model;

class CommentModel extends Model
{
    protected $name = 'comment';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    static $mold = [
        'business' => '商圈',
    ];

    /**
     * 获取评论列表
     * @param string $mold 评论类型:business=商圈
     * @param int $mold_id 数据ID
     * @param int $page
     * @param int $limit
     * @return array
     */
    public static function getPageList(string $mold, int $mold_id, int $page = 1, int $limit = 5): array
    {
        try {
            $where = [
                'status' => 1,
                'mold' => $mold,
                'mold_id' => $mold_id
            ];
            $order = [
                'createtime' => 'desc'
            ];
            $list = model('CommentModel')
                ->field('id,from_uid,to_uid,content,createtime')
                ->where($where)
                ->with([
                    'from' => function($query){
                        $query->field('id,uid,level_id,name,picture');
                        $query->with([
                            'company' => function($q1){
                                $q1->field('id,member_id,company_name');
                            },
                            'level'
                        ]);
                    },
//                    'to' => function($query){
//                        $query->field('id,name,picture');
//                        $query->with(['company' => function($q1){
//                            $q1->field('id,member_id,company_name');
//                        }]);
//                    }
                ])
//                ->withCount('commentreply')
                ->order($order)
                ->whereNull('deletetime')
                ->paginate($limit, false, ['page' => $page]);
            foreach ($list as $row){
                $row->createtime = datetime($row->createtime);
                if(isset($row->from->picture)) {
                    $row->from->picture = cdnurl($row->from->picture);
                }
            }
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => $list
            ];
        }catch (Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => $e->getLine()
            ];
        }
    }

    /**
     * 发布评论
     * @param string $mold 评论类型:business=商圈
     * @param int|string $mold_id 数据ID
     * @param int $user_id
     * @param string $content
     * @return array
     */
    public static function postComment($user_id, $mold, $mold_id, $content): array
    {
        try {
            $member = model('app\admin\model\wdsxh\Member')->where('uid',$user_id)->find();
            if(!$member || $member['status'] != 1){
                throw new Exception('非会员不能发布留言');
            }
            if($member['status'] != 1){
                throw new Exception('会员状态错误');
            }
            $model = '';
            $to_user_id = 0;
            switch ($mold){
                case 'business': $model = 'app\common\model\BusinessModel'; break;
            }

            if(!empty($model)){
                $to_user_id = model($model)->where('id','=',$mold_id)->value('user_id');
            }

            self::create([
                'mold' => $mold,
                'mold_id' => $mold_id,
                'from_uid' => $user_id,
                'to_uid' => $to_user_id,
                'content' => $content,
            ]);
            return [
                'code' => 200,
                'msg' => '操作成功',
                'data' => []
            ];

        }catch (Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }

    /**
     * 修改点赞量
     * @param int $id
     * @param string $type setInc,setDec
     * @return array
     */
    public static function editLikeNum(int $id, $type): array
    {
        try {
            self::where('id',$id)->$type('like',1);

            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => []
            ];
        }catch (Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }

    /**
     * 删除评论
     * @param int $id
     * @return array
     */
    public static function deleteData(int $id): array
    {
        try {
            self::where('id',$id)->setField('deletetime',time());

            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => []
            ];
        }catch (Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }

    public function from()
    {
//        return $this->belongsTo('app\common\model\User', 'from_uid', 'id');
        return $this->belongsTo('app\admin\model\wdsxh\Member', 'from_uid', 'uid');
    }

    public function to()
    {
//        return $this->belongsTo('app\common\model\User', 'to_uid', 'id');
        return $this->belongsTo('app\admin\model\wdsxh\Member', 'to_uid', 'uid');
    }

    public function commentreply()
    {
        return $this->hasMany('app\common\model\CommentReplyModel', 'comment_id', 'id');
    }
}